/*
 * Copyright 2015-2016 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * You may not use this file except in compliance with the terms and conditions set forth
 * in the accompanying LICENSE.TXT file.
 *
 * THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS.  AMAZON SPECIFICALLY DISCLAIMS, WITH
 * RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
 */
package com.amazon.alexa.voicechrome.internal.views.util.wave;

import android.animation.Animator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Animatable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public abstract class AnimatedAmplitudeView extends RelativeLayout implements Animatable {

    private boolean running;
    private Animator animator;
    protected AmplitudeView amplitudeView;

    public AnimatedAmplitudeView(Context context) {
        super(context);
        init();
    }

    public AnimatedAmplitudeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AnimatedAmplitudeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public AnimatedAmplitudeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        amplitudeView = new AmplitudeView(getContext());
        addView(amplitudeView);
        animator = setUpAnimator();
    }

    protected abstract Animator setUpAnimator();

    @Override
    public void start() {
        if (isRunning()) return;
        running = true;
        animator.start();
    }

    @Override
    public void stop() {
        if (!isRunning()) return;
        running = false;
        animator.cancel();
    }

    @Override
    public boolean isRunning() {
        return running;
    }
}
